﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

using DevExpress.XtraEditors.DXErrorProvider;


namespace QuanLyThuNgan
{
    public partial class frm_DM_HetCa : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_HetCa()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMHetCaCTL hetCaCtl = new DMHetCaCTL();
        DMHetCaDTO hetCaDto = new DMHetCaDTO();

        private void frm_DM_HetCa_Load(object sender, EventArgs e)
        {
            DataTable dthetca = hetCaCtl.m_SelectAllDMHetCa();
            gridControlHetCa.DataSource = dthetca;

            //set condition load form----------------------------
            gvHetCa.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvHetCa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlHetCa.DataSource;
          

            if (dt != null)
            {
                for (int i = 0; i < gvHetCa.RowCount; i++)
                {
                    DataRow dr = gvHetCa.GetDataRow(i);
                    gvHetCa.MoveNext();
                    gvHetCa.MovePrev();
                    if (dr["TenHetCa"].ToString() != "")
                    {
                        hetCaDto.STenHetCa = dr["TenHetCa"].ToString().Trim();
                        hetCaDto.BbNgungSuDung = Uit.it_Parse.ToBoolean(dr["bNgungSuDung"].ToString());
                        if (dr["IDHetCa"].ToString() == "")
                        {
                            hetCaCtl.m_InsertDMHetCa(hetCaDto);
                        }
                        else
                        {
                            hetCaDto.IIDHetCa = Uit.it_Parse.ToInteger(dr["IDHetCa"].ToString());
                            hetCaCtl.m_UpdateDMHetCa(hetCaDto);
                        }
                    }
                    
                }
                XtraMessageBox.Show("Lưu thành công");
                
            }
            DataTable dthcload = hetCaCtl.m_SelectAllDMHetCa();
            gridControlHetCa.DataSource = dthcload;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvHetCa.OptionsBehavior.ReadOnly = true;
            gvHetCa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvHetCa.GetDataRow(gvHetCa.FocusedRowHandle);
            if (dr != null)
            {
                hetCaDto.IIDHetCa = Uit.it_Parse.ToInteger(dr["IDHetCa"].ToString());
                hetCaCtl.m_DeleteDMHetCa(hetCaDto);
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dthcLoad = hetCaCtl.m_SelectAllDMHetCa();
            gridControlHetCa.DataSource = dthcLoad;

            gvHetCa.ActiveFilterEnabled = false;
            gvHetCa.ClearColumnsFilter();
            gvHetCa.OptionsFilter.Reset();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvHetCa.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvHetCa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gvHetCa.ActiveFilterEnabled = false;
            gvHetCa.ClearColumnsFilter();
            gvHetCa.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvHetCa.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvHetCa.OptionsBehavior.ReadOnly = true;
            DataTable dtload = hetCaCtl.m_SelectAllDMHetCa();
            gridControlHetCa.DataSource = dtload;
            gvHetCa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvHetCa.ActiveFilterEnabled = false;
            gvHetCa.ClearColumnsFilter();
            gvHetCa.OptionsFilter.Reset();
        }

        //set error when tenhetca is null----------------------------------------------------------------------
        private void gvHetCa_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gvHetCa;
            GridColumn colHetCa = view.Columns["TenHetCa"];
            
            //get value in row---------------------------
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{
            string sTenHetCa = view.GetRowCellValue(e.RowHandle, colHetCa).ToString().ToUpper().Trim();
            if (sTenHetCa == "")
            {
                e.Valid = false;
                view.SetColumnError(colHetCa, "Tên hết ca không được trống");
            }

            //set duyet gia tri trong vong lap for 0-n
            for (int i = 0; i < gvHetCa.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drhc = gvHetCa.GetDataRow(i);
                    string test = drhc["TenHetCa"].ToString();
                    //so sanh gia tri can duyet voi gia tri trong vong lap---
                    if (sTenHetCa.ToString().ToUpper().Trim() == test.ToString().ToUpper().Trim())
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên hết ca không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colHetCa, "Tên hết ca không được trùng");
                        return;
                    }
                }                
            }
                 
           //}
             btnSave.Enabled = true;
        }

        private void gvHetCa_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow= e.FocusedColumn.FieldName;
        }

        //tim ten can search---------------------------------------------
        private void gvHetCa_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }

        //set ten can tim----------------------------------------------------------------
        private void gvHetCa_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvHetCa;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }



    }
}